package dxunderground.utilities.security;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.security.NoSuchAlgorithmException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class SecureEncoderGUI extends JFrame implements ActionListener {
	public static final int FIELD_SIZE = 60; // Size of the folder fields
	public static final String ENCODE_NAME = "Encode";
	public static final String DECODE_NAME = "Decode";
	private JComboBox algorithmField;
	private TextField phraseField, toDecodeField, outputField;
	private TextField placeHolder;
	private JButton encode, decode;

	public SecureEncoderGUI(String[] args) {
		super("Secure Encoder");

		// All the labels will be stored on this panel
		Panel labelPanel = new Panel(new GridLayout(6, 1));

		// Create the labels and add them to the panel. Include blanks to line
		// up with the fields
		JLabel algorithmFieldLabel = new JLabel("Algorithm: ");
		JLabel phraseFieldLabel = new JLabel("Secret Phrase: ");
		JLabel toDecodeFieldLabel = new JLabel("String to encode / decode: ");
		JLabel blank1 = new JLabel("");
		JLabel blank2 = new JLabel("");
		JLabel outputFieldLabel = new JLabel("Output: ");

		labelPanel.add(algorithmFieldLabel);
		labelPanel.add(phraseFieldLabel);
		labelPanel.add(toDecodeFieldLabel);
		labelPanel.add(blank1);
		labelPanel.add(blank2);
		labelPanel.add(outputFieldLabel);

		// The panel for the fields to be saved on
		Panel panel = new Panel(new GridLayout(6, 1));

		// Construct the fields and buttons
		// The combox does not seem to work if there are not enough fields in
		// the list.
		// Seems to work when the list is just larger then the frame
		String[] patternExamples = { "AES", "Blowfish", "DES", "RC2", "RC4", "", "", "" };
		algorithmField = new JComboBox(patternExamples);
		algorithmField.setEditable(true);

		phraseField = new TextField(FIELD_SIZE);
		toDecodeField = new TextField(FIELD_SIZE);
		encode = new JButton(ENCODE_NAME);
		decode = new JButton(DECODE_NAME);
		placeHolder = new TextField();
		placeHolder.setVisible(false);
		outputField = new TextField(FIELD_SIZE);
		outputField.setEditable(false);

		// If there are arguments then default the fields to them.
		// Numbered from top down
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				switch (i) {
				case 0:
					algorithmField.setSelectedItem(args[i]);
					// algorithmField.setText(args[i]);
					break;
				case 1:
					phraseField.setText(args[i]);
					break;
				case 2:
					toDecodeField.setText(args[i]);
					break;
				}
			}
		}

		// Only add events to the buttons
		encode.addActionListener(this);
		decode.addActionListener(this);

		// Add the fields and buttons in order from top down
		panel.add(algorithmField);
		panel.add(phraseField);
		panel.add(toDecodeField);
		// Buttons look better on the same line
		Panel buttons = new Panel(new GridLayout(1, 2));
		buttons.add(encode);
		buttons.add(decode);
		panel.add(buttons);
		panel.add(placeHolder);
		panel.add(outputField);

		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(labelPanel, BorderLayout.WEST);
		this.getContentPane().add(panel, BorderLayout.EAST);

		this.pack();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		String anAlgorithm = (String) algorithmField.getSelectedItem();
		String aSecretPhrase = phraseField.getText();
		String theStringToDecode = toDecodeField.getText();

		if (anAlgorithm.equals("")) {
			outputField.setText("Please enter an algorithm.");
		} else if (aSecretPhrase.equals("")) {
			outputField.setText("Please enter the secret phrase.");
		} else if (aSecretPhrase.equals("")) {
			outputField.setText("Please enter the string to decode.");
		} else {
			try {
				SecureEncoder theEncoder = new SecureEncoder(anAlgorithm, aSecretPhrase);
				if (e.getActionCommand().equals(ENCODE_NAME)) {
					final String encoded = theEncoder.encode(theStringToDecode);
					outputField.setText(encoded);
				} else if (e.getActionCommand().equals(DECODE_NAME)) {
					final String decoded = theEncoder.decode(theStringToDecode);
					outputField.setText(decoded);
				}
			} catch (NoSuchAlgorithmException err) {
				outputField.setText("The algorithm entered does not exist.");
			} catch (Exception err) {
				outputField.setText("Invalid combination. Make sure the secret phrase and the string are correct.");
			}
		}
	}

	public static void main(String[] args) {
		new SecureEncoderGUI(args);
	}
}
